<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>

          <a-col :span="12">
            <a-form-item label="公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['companyName']" placeholder="请输入公司名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="法人姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['legalName',validatorRules.legalName]" placeholder="请输入法人姓名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="法人身份证" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['legalIdCard', validatorRules.legalIdCard]" placeholder="请输入法人身份证号码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="税号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['taxNumber',validatorRules.taxNumber]" placeholder="请输入税号"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="管理员姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['manageName',validatorRules.manageName]" placeholder="请输入管理员姓名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="管理员身份证 " :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['manageIdCard',validatorRules.manageIdCard]" placeholder="请输入管理员身份证"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="管理员电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['managePhone',validatorRules.managePhone]" placeholder="请输入管理员电话"></a-input>
            </a-form-item>
          </a-col>
          <!--<a-col :span="12">-->
            <!--<a-form-item label="管理员邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
              <!--<a-input v-decorator="['manageEmail']" placeholder="请输入管理员邮箱"></a-input>-->
            <!--</a-form-item>-->
          <!--</a-col>-->

        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "ClmCompanyModal",
    components: {
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 7},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {
          legalIdCard: {
            rules: [{
              pattern: /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
              message: '请输入正确的身份证号码!'
            }]
          },

          companyName: {
            rules: [{
              required: true,
              message: '请输入公司名称!'
            }],
          },
          taxNumber: {
            rules: [{
              required: true,
              message: '请输入税号!'
            }],
          },
          manageName: {
            rules: [{
              required: true,
              message: '请输入管理员姓名!'
            }],
          },
          manageIdCard: {
            rules: [{
              required: true,
              message: '请输入管理员身份证!'
            }],
          },
          managePhone: {
            rules: [{
              required: true,
              message: '请输入管理员电话!'
            }],
          },
        },
        url: {
          get: "/online/api/getClmCompany",
        },
        urlEdit:'/online/api/createEsignClmCompany',
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        if(record.update=='Y'){
          this.urlEdit = '/online/api/updateEsignClmCompany';
        }else{
          this.urlEdit = '/online/api/createEsignClmCompany';
        }
        this.form.resetFields();
        this.visible = true;
        this.model = {};
        if (record.id) {
          httpAction(this.url.get, {id: record.id}, "post").then((res) => {
            if (res.success) {
              this.model = Object.assign({}, res.result);
              this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model, 'companyName', 'taxNumber', 'legalName', 'legalIdCard','manageName','manageIdCard','managePhone'))
              })
            }
          })
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = this.urlEdit
            let method = 'put'
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据", formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success('提交成功!');
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row, 'companyName', 'taxNumber', 'legalName', 'legalIdCard','manageName','manageIdCard','managePhone'))
      },


    }
  }
</script>
